﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.CodeDom;
using NRemedy;
using Microsoft.CSharp;
using System.IO;
using System.CodeDom.Compiler;
using System.Diagnostics;

namespace NRemedy.Test
{
    [TestClass]
    public class NRemedy_CodeGenerate_Test
    {
        private void PrintCode(CodeCompileUnit ccu)
        {
            CSharpCodeProvider provider = new CSharpCodeProvider();

            CodeGeneratorOptions options = new CodeGeneratorOptions();
            options.BlankLinesBetweenMembers = false;
            options.IndentString = "\t";

            StringWriter sw = new StringWriter();
            string code = string.Empty;
            try
            {
                provider.GenerateCodeFromCompileUnit(ccu, sw, options);
                sw.Flush();
                code = sw.GetStringBuilder().ToString();
                Debug.WriteLine(code);
            }
            catch { }
            finally
            {
                sw.Close();
            }
        }

        private ARLogInRequestContext login = new ARLogInRequestContext
        {
            Server = "vs31srm01",
            UserName = "parkerz.admin",
            Password = "admin",
            Authentication = ""
        };

        [TestMethod]
        public void Test_Generate_()
        {
            CodeCompileUnit ccu = new CodeCompileUnit();
            GenerateRootNamespace rn = new GenerateRootNamespace();
            rn.Create(ccu);

            Assert.AreEqual<string>(ccu.Namespaces[0].Name, "AR.Entity");

            GenerateImportNamespace imn = new GenerateImportNamespace();
            imn.Create(ccu.Namespaces[0]);

            Assert.AreEqual<int>(ccu.Namespaces[0].Imports.Count, 4);

            GenerateClass gc = new GenerateClass();
            gc.Create(ccu.Namespaces[0], "vAppRequest", "ASB_vAppRequest");


            GenerateField gf = new GenerateField();
            gf.Create(ccu.Namespaces[0].Types[0], "_Int_Str", typeof(Int32));

            Dictionary<string, object> mapping = new Dictionary<string, object>();
            mapping.Add("DatabaseID", 536870914);

            GenerateProperty gp = new GenerateProperty();
            gp.Create(ccu.Namespaces[0].Types[0], "Int_Str", typeof(Int32), mapping);

            PrintCode(ccu);


        }

        [TestMethod]
        public void Test_GenerateClassByFormName()
        {
            CSharpClassGenerator generator = new CSharpClassGenerator(login);

            string code = generator.GenerateClassByFormName("ASB_vAppRequest");

            Debug.Write(code);

        }

    }
}
